<template>
    <div class="qlist" v-scroll="getProduct">
        <div class="container">
            <div class="list-boxs clearfix">
                <div class="list" v-for="(item, j) in phoneList" :key="j">
                    <a :href="'/#/detail/' + item.id">
                        <div class="item">
                            <!-- <span :class="{ 'new-pro': j % 2 == 0 }">新品</span> -->
                            <div class="item-img">
                                <img v-lazy="$img + item.pic" />
                            </div>
                            <div class="item-info">
                                <h3>{{ item.name }}</h3>
                                <p>{{ item.subtitle }}</p>
                                <p class="price" @click="addCart(item.id)">{{ item.price }}元</p>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'category',
    data() {
        return {
            phoneList: [],
            pageNum: 1
        };
    },
    watch: {
        $route(to) {
            this.getProduct(to.params.id);
        }
    },
    mounted() {
        // console.log(this.$route)
        this.getProduct(this.$route.params.id);
    },
    directives: {
        scroll: {
            bind: function (el, binding) {
                window.addEventListener('scroll', function () {
                    // console.log(document.documentElement.scrollTop,document.documentElement.clientHeight, document.documentElement.scrollHeight)
                    if (
                        document.documentElement.scrollTop + document.documentElement.clientHeight >=
                        document.documentElement.scrollHeight - 300
                    ) {
                        let loadData = binding.value;
                        loadData();
                    }
                });
            }
        }
    },
    methods: {
        async getProduct(cateId) {
            if (this.noMore) {
                return false;
            }
            if (!this.loading) {
                this.loading = true;
                let { records, total } = await this.$http.get('/api/home/goods/category/list', {
                    params: { cateId: cateId, pageNum: this.pageNum, pageSize: 20 }
                });
                this.phoneList = this.phoneList.concat(records);
                if (this.phoneList.length >= total) {
                    this.noMore = true;
                } else {
                    this.pageNum++;
                }
                this.loading = false;
            }
        },

        goToCart() {
            this.$router.push('/cart');
        }
    }
};
</script>
<style lang="scss">
@import './../assets/scss/config.scss';
@import './../assets/scss/mixin.scss';
.qlist {
    background-color: #f2f2f2;
    min-height: 60vh;
}
.list-boxs {
    padding: 20px 0 60px;

    margin-bottom: 14px;
    width: 100%;
    .list {
        float: left;
        width: 20%;
        box-sizing: border-box;
        height: 312px;
        padding: 7px;
        margin-bottom: 5px;

        .item {
            background-color: $colorG;
            text-align: center;
            height: 100%;
            box-sizing: border-box;
            padding-top: 15px;

            span {
                display: inline-block;
                width: 67px;
                height: 24px;
                font-size: 14px;
                line-height: 24px;
                color: $colorG;
                &.new-pro {
                    background-color: #7ecf68;
                }
                &.kill-pro {
                    background-color: #e82626;
                }
            }
            .item-img {
                img {
                    width: 100%;
                    height: 195px;
                }
            }
            .item-info {
                h3 {
                    font-size: 14px;
                    color: $colorB;
                    line-height: 14px;
                    font-weight: bold;
                    margin-top: 15px;
                }
                p {
                    color: $colorD;
                    line-height: 13px;
                    margin: 6px auto 13px;
                }
                .price {
                    color: #f20a0a;
                    font-size: $fontJ;
                    font-weight: bold;
                    cursor: pointer;
                    margin-bottom: 0;
                    padding-bottom: 13px;
                    &:after {
                        @include bgImg(22px, 22px, '/imgs/icon-cart-hover.png');
                        content: ' ';
                        margin-left: 5px;
                        vertical-align: middle;
                    }
                }
            }
        }
    }
}
</style>
